import 'package:async/async.dart';
import 'package:boss/common/common.dart';
import 'package:boss/main.dart';
import 'package:boss/models/book_info_list_data.dart';
import 'package:boss/ui/page/takeoutManagement/page/businessSetting/page/timeRange/model/hour_minute.dart';
import 'package:boss/ui/widget/calendar/util/date_util.dart';
import 'package:boss/ui/widget/loader/loader_pull_refresh.dart';
import 'package:flustars/flustars.dart' as flustars;
import 'package:flutter/material.dart';

class BookingHistoryProvider extends ChangeNotifier {
  DateTime startTime = DateUtils.startOfTheDay(DateTime.now());

  String get startTimeText => flustars.DateUtil.formatDate(startTime, format: flustars.DateFormats.y_mo_d);

  DateTime endTime = DateUtils.startOfTheDay(DateTime.now());

  String get endTimeText => flustars.DateUtil.formatDate(endTime, format: flustars.DateFormats.y_mo_d);

  HourMinute beginArrive = HourMinute(0, 0);

  HourMinute endArrive = HourMinute(23, 59);

  DateTime get beginArriveDateTime {
    return DateTime(startTime.year, startTime.month, startTime.day, beginArrive.hour, beginArrive.minute);
  }

  DateTime get endArriveDateTime {
    return DateTime(endTime.year, endTime.month, endTime.day, endArrive.hour, endArrive.minute);
  }

  String status;

  String titleEnum;

  List<BookInfo> bookInfoList = [];

  int totalBookNum;

  int totalPersonNum;

  int get currentPage => (bookInfoList.length / Constant.rows).ceil();

  final GlobalKey<LoaderPullRefreshState> refreshKey = GlobalKey();

  setStartTime(List<DateTime> dateTime) {
    startTime = dateTime[0];
    endTime = dateTime[1];
    notifyListeners();
  }

  Future<Result<List<BookInfo>>> queryBookInfoList(int page) async {
    try {
      final res = await api.queryBookInfoList(
        beginDate: startTimeText,
        endDate: endTimeText,
        beginArriveTime: beginArrive.toString(),
        endArriveTime: endArrive.toString(),
        page: page,
        rows: Constant.rows,
        status: status,
        titleEnum: titleEnum,
      );

      if (!res.successful()) {
        return Result.error(res.msg);
      }

      if (page == 1) {
        bookInfoList = res.data.items;
        notifyListeners();
      } else {
        bookInfoList.addAll(res.data.items);
        notifyListeners();
      }

      return Result.value(res.data.items);
    } catch (e) {
      return Result.error(e);
    }
  }

  Future<bool> queryBookStatisticsInfo() async {
    try {
      final res = await api.queryBookStatisticsInfo(
        beginDate: startTimeText,
        endDate: endTimeText,
        beginArriveTime: beginArrive.toString(),
        endArriveTime: endArrive.toString(),
        rows: Constant.rows,
        status: status,
        titleEnum: titleEnum,
      );

      if (!res.successful()) {
        return false;
      }
      totalPersonNum = res.data['totalPersonNum'];
      totalBookNum = res.data['totalBookNum'];
      notifyListeners();

      return true;
    } catch (e) {
      return false;
    }
  }

  Future<Result<List<BookInfo>>> refresh() async {
    final res = await Future.wait([queryBookInfoList(1), queryBookStatisticsInfo()]);
    final result1 = res[0] as Result<List<BookInfo>>;

    return result1.isValue && res[1] ? result1 : Result.error(Constant.httpErrText);
  }

  Future<Result<List<BookInfo>>> loadMore() async {
    return queryBookInfoList(currentPage + 1);
  }

  setAll({
    DateTime startTime,
    DateTime endTime,
    DateTime beginArrive,
    DateTime endArrive,
    String status,
    String titleEnum,
  }) {
    this.startTime = startTime;
    this.endTime = endTime;

    this.beginArrive.hour = beginArrive.hour;
    this.beginArrive.minute = beginArrive.minute;

    this.endArrive.hour = endArrive.hour;
    this.endArrive.minute = endArrive.minute;

    this.status = status;

    this.titleEnum = titleEnum;

    notifyListeners();
  }
}
